package com.amorcloud.pig.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.amorcloud.pig.MyApp;
import com.amorcloud.pig.R;
import com.amorcloud.pig.adapter.MenuAdapter;
import com.amorcloud.pig.bean.MenuBean;
import com.amorcloud.pig.utils.SharedPreferencesUtils;
import com.amorcloud.pig.widget.CustomToolbar;

import java.util.ArrayList;

public class MainActivity extends BaseActivity {

    CustomToolbar toolbar;
    private GridView menu_grid;
    private ArrayList<MenuBean> menuDatas;
    private MenuAdapter adapter;
    private String farmName = "";
    private Button btnSiginOut;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected void init() {
        setContentView(R.layout.activity_main);
        toolbar = findViewById(R.id.custom_toolbar);
        toolbar.hideBack();

        //Log.e("leihuiming", "144dp = " + px2dip(this, 144));
        menu_grid = findViewById(R.id.menu_grid);
        menuDatas = new ArrayList<>();
        menuDatas.addAll(getMenuList());
        adapter = new MenuAdapter(this, menuDatas);
        //LogUtil.logD(Constant.TAG, "menu_grid=" + (menu_grid == null));
        menu_grid.setAdapter(adapter);

        btnSiginOut = findViewById(R.id.btn_sign_out);
        btnSiginOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApp.lon = "";
                MyApp.lat = "";
                startActivity(new Intent(MainActivity.this, SigninActivity.class));
                MainActivity.this.finish();
            }
        });
    }

    @Override
    public void onBackPressed() {

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { //按下的如果是BACK，同时没有重复
            //Toast.makeText(this,"now_is_back_event", Toast.LENGTH_SHORT).show();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void onResume() {
        super.onResume();
        initFarmName();
    }

    private void initFarmName() {
        farmName = (String) SharedPreferencesUtils.getParam("farmName", "");
        Log.e("leihuiming", "farmName = " + farmName);
        if (!TextUtils.isEmpty(farmName)) {
            toolbar.setTitle(farmName);
        } else {
            toolbar.setTitle("厂商信息");
        }
    }

    public static int px2dip(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    public static ArrayList<MenuBean> getMenuList() {
        ArrayList<MenuBean> datas = new ArrayList<>();
        if (MyApp.currentUser.getIsAdmin().equals("1")) {
            datas.add(new MenuBean("带标信息", R.mipmap.icon_info_disable, PigInfoActivity.class.getName()));
            datas.add(new MenuBean("仔猪断奶", R.mipmap.icon_weaned_disable, null));
            datas.add(new MenuBean("猪只盘点", R.mipmap.icon_check_disable, CheckActivity.class.getName()));
            datas.add(new MenuBean("猪只销售", R.mipmap.icon_sell_disable, SellActivity.class.getName()));
            datas.add(new MenuBean("数据上传", R.mipmap.icon_upload_disable, UploadActivity.class.getName()));
            datas.add(new MenuBean("系统设置", R.mipmap.icon_settings, SettingsActivity.class.getName()));
        } else {
            datas.add(new MenuBean("带标信息", R.mipmap.icon_info, PigInfoActivity.class.getName()));
            datas.add(new MenuBean("仔猪断奶", R.mipmap.icon_weaned, null));
            datas.add(new MenuBean("猪只盘点", R.mipmap.icon_check, CheckActivity.class.getName()));
            datas.add(new MenuBean("猪只销售", R.mipmap.icon_sell, SellActivity.class.getName()));
            datas.add(new MenuBean("数据上传", R.mipmap.icon_upload, UploadActivity.class.getName()));
            datas.add(new MenuBean("系统设置", R.mipmap.icon_settings, SettingsActivity.class.getName()));
        }

        return datas;
    }

}
